home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / POV-Ray 3.0.2 / src / MacSource / VolsPaths.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-14  |  8.3 KB  |  283 lines  |  [TEXT/CWIE]

  1. /*==============================================================================
  2. Project:    POV-Ray
  3.  
  4. Version:    3
  5.  
  6. File:    VolsPaths.c
  7.  
  8. Description:
  9.     Various volume and pathname handling routines.
  10. ------------------------------------------------------------------------------
  11. Author:
  12.     Eduard [esp] Schwan
  13. ------------------------------------------------------------------------------
  14.     from Persistence of Vision(tm) Ray Tracer
  15.     Copyright 1996 Persistence of Vision Team
  16. ------------------------------------------------------------------------------
  17.     NOTICE: This source code file is provided so that users may experiment
  18.     with enhancements to POV-Ray and to port the software to platforms other 
  19.     than those supported by the POV-Ray Team.  There are strict rules under
  20.     which you are permitted to use this file.  The rules are in the file
  21.     named POVLEGAL.DOC which should be distributed with this file. If 
  22.     POVLEGAL.DOC is not available or for more info please contact the POV-Ray
  23.     Team Coordinator by leaving a message in CompuServe's Graphics Developer's
  24.     Forum.  The latest version of POV-Ray may be found there as well.
  25.  
  26.     This program is based on the popular DKB raytracer version 2.12.
  27.     DKBTrace was originally written by David K. Buck.
  28.     DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
  29. ------------------------------------------------------------------------------
  30. Change History:
  31.     941221    [esp]    Created
  32. ==============================================================================*/
  33.  
  34. #define VOLSPATHS_C
  35.  
  36. /*==== my header =====*/
  37. #include "VolsPaths.h"
  38.  
  39.  
  40. /*==== Macintosh-specific headers ====*/
  41. #include <Types.h>
  42. #include <Files.h>
  43. #include <Resources.h>
  44. #include <errors.h>            /* dupFNErr, etc */
  45. #include <Memory.h>            /* NewPtr, etc */
  46. #include <string.h>            /* strcpy/cat */
  47. #include <strings.h>        /* p2cstr */
  48. #include <TextUtils.h>        /* Munger */
  49.  
  50.  
  51. /*==== Standard C headers ====*/
  52.  
  53.  
  54. /*==== General definitions ====*/
  55.  
  56.  
  57. /*==== Global variables (external scope) ====*/
  58.  
  59.  
  60. /*==== Global variables (local scope) ====*/
  61.  
  62.  
  63. // ---------------------------------------------------------------------
  64. // VRef2VolName
  65. // ---------------------------------------------------------------------
  66. OSErr VRef2VolName(short theVRef, StringPtr theVolName)
  67. {
  68.     OSErr            anError;
  69.     HVolumeParam    pbVInfo;
  70.  
  71.     pbVInfo.ioCompletion = NULL;
  72.     theVolName[0] = '\0';
  73.     pbVInfo.ioNamePtr = theVolName;
  74.     pbVInfo.ioVRefNum = theVRef;
  75.     pbVInfo.ioVolIndex = 0; // use VRefNum only
  76.     anError = PBHGetVInfoSync((HParmBlkPtr)&pbVInfo);
  77.  
  78.     return anError;
  79. } // VRef2VolName
  80.  
  81.  
  82. // ---------------------------------------------------------------------
  83. // VolName2VRef
  84. // ---------------------------------------------------------------------
  85. OSErr VolName2VRef(StringPtr myPVolName, short * theVRefPtr)
  86. {
  87.     OSErr            anError = noErr;
  88.     Boolean            foundIt = false;
  89.     short            theDriveNum;
  90.     short            theVRefNum;
  91.     HParamBlockRec    aParamBlk;
  92.     char            myCVolName[31];
  93.     Str31            theVolName;
  94.  
  95.     // convert passed vol name from P 2 C string for compare
  96.     BlockMove(myPVolName, myCVolName, myPVolName[0]+1);
  97.     p2cstr((StringPtr)myCVolName);
  98.  
  99.     // loop through all mounted volumes, see if myPVolName matches one
  100.     for (theDriveNum = 1; !foundIt && (anError==noErr); theDriveNum++)
  101.     {
  102.         theVolName[0] = '\0';
  103.         aParamBlk.volumeParam.ioNamePtr = (StringPtr)&theVolName;
  104.         aParamBlk.volumeParam.ioVolIndex = theDriveNum;
  105.         
  106.         anError = PBHGetVInfoSync((HParmBlkPtr)&aParamBlk);
  107.         if (anError == noErr)
  108.         {
  109.             p2cstr(theVolName);
  110.             if (strcmp((char*)theVolName, myCVolName)==0)
  111.             {
  112.                 foundIt = true;
  113.                 theVRefNum = aParamBlk.volumeParam.ioVRefNum;
  114.             }
  115.         }
  116.     }
  117.  
  118.     if (foundIt)
  119.         *theVRefPtr = theVRefNum;
  120.     else
  121.         *theVRefPtr = 0;
  122.  
  123.     if (anError == nsvErr) // no such vol, just bumped end of volume list
  124.         anError = noErr;
  125.  
  126.     return anError;
  127. } // VolName2VRef
  128.  
  129.  
  130. // ---------------------------------------------------------------------
  131. // PathNameFromDirID
  132. // ---------------------------------------------------------------------
  133. /*
  134. Function:    PathNameFromDirID
  135.  
  136. Parameters:
  137.     aDirID        the directory ID to start at
  138.     avRefNum    the volume ref # to start at
  139.     aPathStyle    The style of pathname to format (based on OS)
  140.     aPath        the final path string is placed in this buffer
  141.  
  142. Return Value:
  143.     zero is returned if OK, else an error is returned
  144.  
  145. Purpose/Description:
  146.     PathNameFromDirID takes a real vRefNum and a DirID and returns
  147.     the full pathname that corresponds to it.  It does this by calling
  148.     _PBGetCatInfo for the given directory and finding out its name and
  149.     the DirID of its parent. It then performs the same operation on the
  150.     parent, sticking its name onto the beginning of the first directory.
  151.     This whole process is continued until we have processed everything up
  152.     to the root directory (identified with a DirID of 2). 
  153. */
  154. OSErr PathNameFromDirID(const long            aDirID,
  155.                         const short            avRefNum,
  156.                         char                * aPath)
  157. {
  158.     Handle        strHandle = NULL;
  159.     OSErr        anError = noErr;
  160.     long        currDirID = 0,
  161.                 nextDirID = 0;
  162.     Size        theLen = 0;        // GetHandleSize
  163.     char        tempName[64];    // directory or volume name
  164.     CInfoPBRec    aPBblock;
  165.  
  166.     /* create an empty string handle for Munger */
  167.     strHandle = NewHandle(0);
  168.  
  169.     if (strHandle == NULL)
  170.     {
  171.         anError = MemError();
  172.     }
  173.     else
  174.     {
  175.         *aPath = '\0';
  176.         aPBblock.dirInfo.ioCompletion    = NULL;
  177.         aPBblock.dirInfo.ioNamePtr        = (StringPtr)tempName;    // temp destination buffer
  178.         nextDirID                        = aDirID;    // start at DirID passed in..
  179.  
  180.         /* walk up the directories, collecting dir names as we go, until we hit the top (volname) */
  181.         do {
  182.             
  183.             aPBblock.dirInfo.ioVRefNum        = avRefNum;
  184.             aPBblock.dirInfo.ioFDirIndex    = -1; // ignore ioNamePtr input, use ioDirID
  185.             aPBblock.dirInfo.ioDrDirID        = nextDirID;
  186.  
  187.             anError = PBGetCatInfoSync(&aPBblock);
  188.             currDirID    = aPBblock.dirInfo.ioDrDirID;    // remember current
  189.             nextDirID    = aPBblock.dirInfo.ioDrParID;    // find parent for next loop
  190.  
  191.             if (anError != noErr)
  192.                 break;
  193.  
  194.             /* Insert a separator in front of the Directory Entry */
  195.             Munger(strHandle, 0L, NULL, 0L, ":", 1L);
  196.  
  197.             /* Insert the next directory/volume name into the front of the string */
  198.             Munger(strHandle, 0L, NULL, 0L, &(tempName[1]), (long)tempName[0]);
  199.  
  200.             /* NOTE: fsRtDirID is defined in Files.h */
  201.         } while (currDirID != fsRtDirID);
  202.  
  203.         /* now move the string into the parameter */
  204.         theLen = GetHandleSize(strHandle);
  205.         if (!anError && theLen)
  206.         {
  207.             BlockMove(&(**strHandle), aPath, theLen);
  208.             anError = MemError();
  209.         }
  210.         aPath[theLen] = '\0';    /* set C string length */
  211.     
  212.         /* dispose of temp storage */
  213.         DisposeHandle(strHandle);
  214.  
  215.     } /*else NULL*/
  216.  
  217.     return(anError);
  218.  
  219. } /* PathNameFromDirID */
  220.  
  221.  
  222. // ---------------------------------------------------------------------
  223. // PathNameFromWD
  224. // ---------------------------------------------------------------------
  225. /*
  226. Function:    PathNameFromWD
  227.  
  228. Parameters:
  229.     avRefNum    the volume ref # to start at (Assumes WD is set)
  230.     aPathStyle    The style of pathname to format (based on OS)
  231.     aPath        the final path string is placed in this buffer
  232.  
  233. Return Value:
  234.     Describe the value returned, or void
  235.  
  236. Purpose/Description:
  237.     PathNameFromWD takes an HFS Working Directory ID and returns
  238.     the full pathname that corresponds to it.  It does this by calling
  239.     _PBGetWDInfo to get the vRefNum and DirID of the real directory.
  240.     It then calls PathNameFromDirID and returns its result.  
  241. */
  242. OSErr PathNameFromWD(    const short            avRefNum,
  243.                         char                * aPath)
  244. {
  245.     WDPBRec    aPBblock;
  246.     OSErr    anError    = noErr;
  247.  
  248. /*
  249.  PBGetWDInfo has a bug under A/UX 1.1.  If vRefNum is a real vRefNum
  250.  and not a wdRefNum, then it returns garbage.  Since A/UX has only 1
  251.  volume (in the Macintosh sense) and only 1 root directory, this can
  252.  occur only when a file has been selected in the root directory (/).
  253.  So we look for this and hard code the DirID and vRefNum.
  254.  */
  255. #if defined(AUX)
  256.     if ((aPathStyle == ePathTypeAUX) && (avRefNum == -1))
  257.     {
  258.         anError = PathNameFromDirID(fsRtDirID, -1, aPath);
  259.     }
  260.     else
  261. #endif
  262.     {
  263.         aPBblock.ioCompletion = NULL;
  264.         aPBblock.ioNamePtr = NULL;
  265.         aPBblock.ioVRefNum = avRefNum;
  266.         aPBblock.ioWDIndex = 0;
  267.         aPBblock.ioWDProcID = 0;
  268.     
  269.         /* Change the Working Directory number in vRefnum into a real vRefnum */
  270.         /* and DirID. The real vRefnum is returned in ioVRefnum, and the real */
  271.         /* DirID is returned in ioWDDirID. */
  272.     
  273.         anError = PBGetWDInfoSync(&aPBblock);
  274.         if (anError == noErr)
  275.             anError = PathNameFromDirID(aPBblock.ioWDDirID,
  276.                                         aPBblock.ioWDVRefNum,
  277.                                         aPath);
  278.     }
  279.  
  280.     return(anError);
  281.  
  282. } /* PathNameFromWD */
  283.